<template>
  <div class="mod-user">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item
        >客户
        <el-select
          v-model="dataForm.value1"
          placeholder="请选择"
          @visible-change="checked1($event)"
          @change="change1($event)"
        >
        <el-option label="全选" value="全选"></el-option>
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.supplier"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        >仓库
        <el-select
          v-model="dataForm.value"
          placeholder="请选择"
          @visible-change="checked($event)"
          @change="change($event)"
        >
        <el-option label="全选" value="全选"></el-option>
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <div class="block">
          单据日期：
          <el-date-picker
            v-model="value1"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="getTime"
          >
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!-- <el-button type="success"
          ><i class="el-icon-download"></i>导出</el-button
        >
        <el-button type="primary"
          ><i class="el-icon-printer"></i>打印</el-button
        > -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="mName"
        header-align="center"
        align="center"
        label="商品名称"
      >
      </el-table-column>
      <el-table-column
        prop="model"
        header-align="center"
        align="center"
        label="商品型号"
      >
      </el-table-column>
      <el-table-column
        prop="categoryName"
        header-align="center"
        align="center"
        label="商品类型"
      >
      </el-table-column>
      <el-table-column
        prop="numSum"
        header-align="center"
        align="center"
        width="180"
        label="入库数量"
      >
      </el-table-column>
      <el-table-column
        prop="priceSum"
        header-align="center"
        align="center"
        label="入库金额"
      >
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./incount-add-or-update copy";
export default {
  data() {
    return {
      dataForm: {
        value: "",
        value1: "",
        name: "",
        model: "",
        time: "2020-11",
        id: "",
        id1: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      allCurrentAmount: "",
      allMonthAmount: "",
      options: [],
      options1: [],
      value1: [],
      beginTime: "",
      endTime: "",
      depotIds: [],
    };
  },
  components: {
    AddOrUpdate,
  },
  activated() {
    this.getdepot();
  },
  methods: {
    getTime() {
      this.beginTime = this.timestampToTime(this.value1[0].getTime());
      this.endTime = this.timestampToTime(this.value1[1].getTime());
      console.log(this.beginTime, this.endTime);
    },
    // 时间戳转成时间
    timestampToTime(timestamp) {
      var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var Y = date.getFullYear() + "-";
      var M =
        (date.getHours() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var D = date.getDate() + " ";
      var h = date.getHours() + ":";
      var h = date.getHours() + ":";
      var m = date.getMinutes() + ":";
      var s = date.getSeconds();
      return Y + M + D + h + m + s;
    },
    checkmonth(m) {
      console.log(m);
      this.dataForm.time = this.timestampToTime(m.getTime());
      console.log(this.dataForm.time);
    },
    change(id) {
      this.dataForm.id = id;
      // console.log(this.dataForm.id)
    },
    change1(id) {
      this.dataForm.id1 = id;
      // console.log(this.dataForm.id1)
    },
    checked(e) {
      if (e) {
        this.getdepot();
      }
    },
    getdepot(){
      this.$http({
          url: this.$http.adornUrl("/depot/getAllList"),
          method: "get",
        }).then(({ data }) => {
          // console.log(data.data);
          if (data && data.code === 0) {
            this.options = data.data;
            this.depotIds=[];
            this.options.map((item)=>{
              // console.log(item.id)
              this.depotIds.push(item.id);
            })
            // console.log(this.depotIds.toString())
            this.getDataList()
          }
        });
    },
    checked1(e) {
      if (e) {
      let UBType = 'UserCustomer'
      let UBKeyId = 63
        this.$http({
         url: this.$http.adornUrl(`/supplier/findBySelect_cus`),
        method: "post",
        data: this.$http.adornData({
                UBType,
                UBKeyId,
              }),
        }).then(({ data }) => {
          // console.log(data.data);
          if (data && data.code === 0) {
            this.options = data.data;
          }
        });
      }
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      if(this.dataForm.id=="全选"){
        this.dataForm.id=""
        console.log(222)
      }
      if(this.dataForm.id1=="全选"){
        this.dataForm.id1=""
      }
      this.$http({
        url: this.$http.adornUrl("/depotHead/findInOutMaterialCount"),
        method: "get",
        params: this.$http.adornParams({
          currentPage: this.pageIndex,
          pageSize: this.pageSize,
          organId: this.dataForm.id1,
          projectId: this.dataForm.id,
          depotIds: this.depotIds.toString(),
          beginTime: this.beginTime,
          endTime: this.endTime,
          type: "出库",
        }),
      }).then(({ data }) => {
        // console.log(data.data.list);
        if (data && data.code === 0) {
          this.dataList = data.data.list;
          this.totalPage = data.data.list.length;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      // console.log(id)
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var userIds = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.userId;
          });
      this.$confirm(
        `确定对[id=${userIds.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/sys/user/delete"),
            method: "post",
            data: this.$http.adornData(userIds, false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },

    // 重置
    resetDataList() {
      // console.log(11)
      this.dataForm.userName = null;
      this.dataForm.userID = null;
    },
  },
};
</script>
